import java.awt.*;
import java.awt.event.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.EventListener;
import javax.swing.*;

@SuppressWarnings("serial")
public class ObjectDraw3 extends JApplet  {

  protected DrawingCanvas canvas;
  protected EventListener listener;
  protected boolean isApplet = false;

  /* Constructors  */

  public ObjectDraw3(boolean isApplet) { 
    this.isApplet = isApplet;
    if (!isApplet) {
      init();
    }
  }

  public ObjectDraw3() {
    /* invoked as Applet */
    this(true);
  }

  public void init() {
    getContentPane().setLayout(new BorderLayout());
    canvas = createDrawingCanvas();
    getContentPane().add(canvas, BorderLayout.CENTER);
    listener = createDrawingCanvasListener(canvas);
    canvas.addDrawingCanvasListener(listener);
    ControlPanel controlPanel = createControlPanel();
    getContentPane().add(controlPanel, BorderLayout.SOUTH);
    EventListener controlPanelListener =
    createControlPanelListener(canvas);
    controlPanel.addControlPanelListener(controlPanelListener);
    ToolList toolList = createToolList();
    ToolBar toolBar = createToolBar(toolList);
    getContentPane().add(toolBar, BorderLayout.WEST);
    MenuBar menuBar = createMenuBar(toolList);
    getContentPane().add (menuBar, BorderLayout.NORTH);
    
    EventListener listener = createKeyboardListener(canvas);
    (canvas).addKeyboardListener(listener);
  }


  /*Factory methods  */
  protected DrawingCanvas createDrawingCanvas() {
    return new DrawingCanvas();
  }

  protected EventListener
             createDrawingCanvasListener(DrawingCanvas c) {
    return new DrawingCanvasListener(c);
  }
  protected ControlPanel createControlPanel() {
	    return new ControlPanel();
  }

  protected EventListener createControlPanelListener(DrawingCanvas canvas) {
    return new ControlPanelListener(canvas);
  }

  protected ToolBar createToolBar(ToolList toolList) {
	return new ToolBar(toolList);
  }

  protected MenuBar createMenuBar(ToolList toolList) {
    return new MenuBar(toolList);
  }
  
  protected EventListener createKeyboardListener(DrawingCanvas canvas) {
	    return new KeyboardListener(canvas);
	  }


  /* Main method  */

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setTitle("ObjectDraw3");
    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add(new ObjectDraw3(false),
			      BorderLayout.CENTER);
    frame.addWindowListener(new AppCloser());
    frame.pack();
    frame.setSize(600, 400);
    frame.setVisible(true);
  }
  protected ToolList createToolList() {
	    ToolList actions = new ToolList();
	    actions.add(
	        new ToolListener("Freehand",
		    getImageIcon("freehand.jpg"),
		    "freehand drawing tool",
		    canvas,
			new FreehandTool(canvas)));
	    actions.add(
			new ToolListener("Line",
			getImageIcon("line.jpg"),
			"Line drawing tool",
			canvas,
			new LineTool(canvas)));
	    actions.add(
			new ToolListener("Rectangle",
			getImageIcon("rectangle.jpg"),
			"Rectangle drawing tool",
			canvas,
			new RectangleTool(canvas)));
	    actions.add(
		    new ToolListener("Oval",
		    getImageIcon("oval.jpg"),
			"Oval drawing tool",
			canvas,
			new OvalTool(canvas)));
	    actions.add(
			new ToolListener("Eraser",
			getImageIcon("eraser.jpg"),
			"Eraser drawing tool",
			canvas,
			new EraserTool(canvas)));
        actions.add(
    		new ToolListener("Text",
    		getImageIcon("text.jpg"),
    		"text drawing tool",
            canvas,
    		new TextTool(canvas)));
        actions.add(
			new ToolListener("Selection",
			getImageIcon("eraser.jpg"),
			"object selection tool",
	        canvas,
			new SelectTool(canvas)));

	    return actions;
	  }
  protected ImageIcon getImageIcon(String fileName) {
	    if(isApplet) {
	      try {
	        URL url = new URL(getCodeBase(), fileName);
	        return new ImageIcon(url);
	      }
	      catch(MalformedURLException e) {
	           return null;
	      }
	    }
	    else {
	      return new ImageIcon(fileName);
	    }
	  }
  /* Inner class AppCLoser for terminating application  */
  /* when Close Window button of frame is clicked       */
   
   static class AppCloser extends WindowAdapter  {
     public void windowClosing(WindowEvent e) {
       System.exit(0);
    }
  }
}
